/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.davidhodin.easymoney.data;

import com.davidhodin.easymoney.data.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author David
 */
public class MouvementJpaController implements Serializable {

    public MouvementJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Mouvement mouvement) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(mouvement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Mouvement mouvement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            mouvement = em.merge(mouvement);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = mouvement.getId();
                if (findMouvement(id) == null) {
                    throw new NonexistentEntityException("The mouvement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Mouvement mouvement;
            try {
                mouvement = em.getReference(Mouvement.class, id);
                mouvement.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The mouvement with id " + id + " no longer exists.", enfe);
            }
            em.remove(mouvement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Mouvement> findMouvementEntities() {
        return findMouvementEntities(true, -1, -1);
    }

    public List<Mouvement> findMouvementEntities(int maxResults, int firstResult) {
        return findMouvementEntities(false, maxResults, firstResult);
    }

    public List<Mouvement> findMouvementEntities(String compte, String categorie) {
        EntityManager em = getEntityManager();
        try {
            Query createQuery;
            if (categorie != null) {
                createQuery = em.createQuery("SELECT c FROM Mouvement c WHERE c.compte = ?1 AND c.categorie = ?2 ORDER BY c.dateOperation DESC");
                createQuery.setParameter(1, compte);
                createQuery.setParameter(2, categorie);
            } else {
                createQuery = em.createQuery("SELECT c FROM Mouvement c WHERE c.compte = ?1 ORDER BY c.dateOperation DESC, c.categorie DESC");
                createQuery.setParameter(1, compte);
            }
            return createQuery.getResultList();
        } catch (Exception e) {
            System.err.println("Erreur de récupération de la liste des mouvements : " + e.toString());
        } finally {
            em.close();
        }
        return null;
    }

    private List<Mouvement> findMouvementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Mouvement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Mouvement findMouvement(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Mouvement.class, id);
        } finally {
            em.close();
        }
    }

    public int getMouvementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Mouvement> rt = cq.from(Mouvement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
